package ex2;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @AUTHOR 尹书岿
 * @DATE 2024/12/10 13:24
 * @DESCRIBE
 */
public class WordCount {
    public static void main(String[] args) {
        String sentence = "the quick brown fox jumps over the lazy dog";
        String[] words = sentence.split(" ");
        System.out.println(Arrays.toString(words));

        //用map来统计单词出现的次数  key为单词  value为出现的次数
        Map<String,Integer> map = new HashMap<>();
        for (String word : words) {
            if(map.containsKey(word)){
                int count = map.get(word);
                count++;
                map.put(word,count);
            }
            else{
                map.put(word,1);
            }
        }
        for (Map.Entry<String,Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }
}
